function getUrlParam(key) {
  const tmpRes = window.location.search.match(new RegExp(`[?&]${key}=([^&]+)`, 'i'));
  let result;
  if (tmpRes === null || tmpRes.length < 1) {
    result = '';
  } else {
    [, result] = tmpRes;
  }
  return result;
};
const wishs = [
  'wine',
  'alcohol',
  'liquor',
  'brandy',
  'vodka',
  'tequila',
  'spirit',
  'whisky ',
  'beer',
  'rum',
  'martell',
  'remy Martin',
  'F.O.V',
  'Bisquit',
  'Polignae',
  'coffeee ljqueu',
  'blue curacao',
  'coin treeou',
  'louis ',
  'napolone',
  'augier ',
  'hine',
  'otard',
  'applejack',
  'bass',
  'beaujolais',
  'bishop',
  'bitter',
  'blackstrap',

]
new Vue({
  el: '#app',
  data() {
    return {
      isOpenedTop: false,
      items: [],
      isMusicPlay: true,
      lock: false,
      startX: 0,
      endX: 0,
      currentPage: 0,
      totalPage: 0,
    };
  },
  created() {
    const liveId = getUrlParam('liveId');
    const Http = new XMLHttpRequest();
    const url = '/hmGiftController/heartWallList?type=xin&is_delete=0&splid=' + liveId;
    Http.open('GET', url);
    Http.send();
    let that = this;
    Http.onreadystatechange = function () {
      if (this.readyState == 4) {
        const tmpList = JSON.parse(Http.responseText).data.list;
        const tmpLen = tmpList.length;
        const tmpPhotoList = [];
        for (let i = 0; i < tmpLen; i += 1) {
          tmpPhotoList.push(tmpList[i].photo_start_url);
        }
        for (let i = 0; i < tmpLen; i += 1) {
          that.items.push({
            img1: tmpPhotoList[i],
            // img2: tmpPhotoList[i + 1],
            // img3: tmpPhotoList[i + 2],
            title: wishs[i],
            isOpen: false,
          });
        }
        console.log(that.items);
        that.totalPage = that.items.length;
        that.loopAutoShow();
      }
    }
  },
  mounted() {
    wx.config({
      // 配置信息, 即使不正确也能使用 wx.ready
      debug: false,
      appId: '',
      timestamp: 1,
      nonceStr: '',
      signature: '',
      jsApiList: [],
    });
    wx.ready(() => {
      this.isMusicPlay = true;
      this.$refs.audioDom.play();
    });
  },
  methods: {
    topOpen() {
      this.isOpenedTop = !this.isOpenedTop;
      if (this.isOpenedTop) {
        this.currentPage = 1;
      } else {
        this.currentPage = 0;
      }
    },

    open(idx, isOpen) {
      if (this.isOpenedTop) {
        this.items[idx].isOpen = !isOpen;
      }
      this.currentPage += 1;
    },

    reset() {
      this.items.forEach(item => (item.isOpen = false));
      this.isOpenedTop = false;
      this.currentPage = 0;
    },
    switchMusic() {
      this.isMusicPlay = !this.isMusicPlay;
      if (this.isMusicPlay) {
        this.$refs.audioDom.play();
      } else {
        this.$refs.audioDom.pause();
      }
    },
    handleTouchStart(e) {
      console.log(e);
      if (!this.lock) {
        this.lock = true;
        this.startX = e.touches[0].pageX;
      }
    },
    handleTouchEnd(e) {
      console.log(e);
      this.isOperate = true;
      this.endX = e.changedTouches[0].pageX;
      if (this.endX < this.startX) {
        console.log(this.currentPage, this.totalPage);
        if (this.currentPage === this.totalPage) {
          this.reset();
        } else {
          if (this.items[this.currentPage - 1]) {
            this.open(this.currentPage - 1, this.items[this.currentPage - 1].isOpen);
          }
        }
      }
      this.lock = false;
    },
    handleTouchStartTop(e) {
      console.log(e);
      if (!this.lock) {
        this.lock = true;
        this.startX = e.touches[0].pageX;
      }
    },
    handleTouchEndTop(e) {
      console.log(e);
      this.isOperate = true;
      this.endX = e.changedTouches[0].pageX;
      if (this.endX < this.startX) {
        this.topOpen();
      }
      this.lock = false;
    },
    // 自动翻页(无操作6s后自动翻页)
    loopAutoShow() {
      this.count = -1;
      const emptyOperateTime = 3; // 空操作时间
      // 重置定时器
      const resetInterval = () => {
        this.count = -1;
      };
      // 定时器
      this.tmpInterval = setInterval(() => {
        this.count += 1;
        console.log(this.count);
        // 主动翻页则清空定时器
        if (this.isOperate) {
          clearInterval(this.tmpInterval);
          return;
        }
        if (this.count >= emptyOperateTime) {
          if (this.currentPage === 0) {
            this.topOpen();
          } else {
            if (this.currentPage === this.totalPage) {
              this.reset();
              resetInterval();
            } else {
              this.open(this.currentPage - 1, this.items[this.currentPage - 1].isOpen);
            }
          }

        }
      }, 1000);
    }
  }
})